
#include "eventlistener.h"
#include <iostream>

EventListener::EventListener(QObject *parent)
    : QObject(parent)
{

}

EventListener::~EventListener()
{

}
// 监听框架事件
void EventListener::onFrameworkEvent(const ctkPluginFrameworkEvent& event)
{
    if (!event.isNull()) {
        QSharedPointer<ctkPlugin> plugin = event.getPlugin();
        std::cout << "FrameworkEvent: [" << plugin->getSymbolicName().toStdString() << "]"
            << event.getType() << event.getErrorString().toStdString() << std::endl;
    } else {
        std::cout  << "The framework event is null" << std::endl;
    }
}

// 监听插件事件
void EventListener::onPluginEvent(const ctkPluginEvent& event)
{
    if (!event.isNull()) {
        QSharedPointer<ctkPlugin> plugin = event.getPlugin();
        std::cout << "PluginEvent: [" << plugin->getSymbolicName().toStdString() << "]"
                  << event.getType() << std::endl;
    } else {
        std::cout  << "The plugin event is null" << std::endl;
    }
}

// 监听服务事件
void EventListener::onServiceEvent(const ctkServiceEvent &event)
{
    if (!event.isNull()) {
        ctkServiceReference ref = event.getServiceReference();
        QSharedPointer<ctkPlugin> plugin = ref.getPlugin();
        for(const auto &pluginD : ref.getUsingPlugins())
        {
            std::cout << "ServiceEvent: [" << plugin->getSymbolicName().toStdString() << "]"
                      << event.getType() << pluginD->getSymbolicName().toStdString() << std::endl;
        }
    } else {
        std::cout  << "The service event is null" << std::endl;
    }
}